package rickyGame.resource
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.system.ApplicationDomain;
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;
	
	import mx.core.BitmapAsset;
	
	/**
	 * 动画播放专用
	 */
	public class BitMapCache extends Cache
	{
		private static var _inst:BitMapCache;
		
		public static function get inst():BitMapCache
		{
			if(!_inst)
				_inst=new BitMapCache();
			return _inst;
		}
		
		public function BitMapCache()
		{
			super();
		}
	
		/**
		 * 得到缓存中的位图
		 * className类名,sceneName场景名 ,matrix镜像
		 */
		public function getBitMapData(className:String,sceneName:String="",matrix:Matrix=null):BitmapData
		{
			var key:String=sceneName+className;
			if(_resMap[key])
				return _resMap[key];				
			else
			{
				var res:BitmapData;

				if(className)
				{
					try{
						
						var bmpclass:Class;
						var bitm:*;//资源实例
						var bitdate:BitmapData;
						var re:Rectangle;
						
						bmpclass = ApplicationDomain.currentDomain.getDefinition(className) as Class;// ApplicationDomain.currentDomain.getDefinition getDefinitionByName
						
						bitm=new bmpclass();
						if(bitm is Bitmap)
							bitdate=bitm.bitmapData;
						else
							bitdate=bitm;
//						
//						re =bitdate.getColorBoundsRect(0xff000000,0x00000000,false);//得到非透明区域
//						bitdate.rect.x=re.x;
//						bitdate.rect.y=re.y;
//						bitdate.rect.width=re.width;
//						bitdate.rect.height=re.height;
						if(matrix)
						{
							var bit:BitmapData=new BitmapData(bitdate.width,bitdate.height,true,0x00);
							matrix.tx=bitdate.width;
							bit.draw(bitdate,matrix);
							res=bit;
						}
						else
							res=bitdate;
						bitm=null;
						_resMap[key]=res;
					}
					catch(e:Error){
						trace(e+"类名"+className+":bitMapCachesh行77");
					}
				}
				return res;
			}
		}
		
		/**
		 * 清除缓存
		 */
		override public  function remove(key:String):void
		{
			if(_resMap[key])
				_resMap[key].dispose();
			super.remove(key);
		
		}
		
	}
}